﻿using UnityEngine;
using System.Collections;
using UnityEngine.Events;

//单位类
public class Unit : MonoBehaviour {

    
	public Animator ani;//动画播放器
	public GameObject bulletPrefab;//子弹预制体
	public GameObject firepoint;//子弹生成点
	public float hp;//当前血量
	public float maxHp;//最大血量
	public float speed=5f;//移动速度
	public float fireRate=5f;//射击速率
	protected float fireTimer=0f;//射击计时器
	public bool isDeath=false;//是否死亡
	protected bool isflying=false;//是否飞行

    protected virtual void Start()
    {
        hp = maxHp;
    }

    //静止
	public void Idle()
	{
		ani.SetTrigger("Idle");
	}

    //飞行
	public void Fly()
	{
		ani.SetTrigger ("Fly");
		isflying = true;
	}

    //死亡
	public virtual void Death()
	{
		if (hp <= 0)
		{
            if (Random.Range(0, 100) < 5)
                event_manager.Instance.dispatch_event("prop", this.transform.position);
            ani.SetTrigger ("Death");
			Destroy (gameObject, 0.1f);
			isDeath = true;
		}
	}

    //自动射击
	public void Fire()
	{
		if (fireTimer > 1 / fireRate) 
		{
			Instantiate (bulletPrefab, firepoint.transform.position, Quaternion.identity);
			fireTimer=0f;
		}
	}

}
